﻿<Window x:Class="gsview.MainWindow"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="gsview" Height="850" Width="714" UseLayoutRounding="True" 
        KeyDown="OnKeyDownHandler" Closing="AppClosing">
    <!-- UseLayoutRounding needed to avoid funny interpolation effects on pages -->

    <Window.Resources>
        <DataTemplate x:Key="PageTemplate">
            <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="{Binding Height}" Width="{Binding Width}" Margin="0,0,0,0" 
                    MouseLeftButtonDown="PageMouseDown" MouseMove="PageMouseMove" MouseLeftButtonUp="PageLeftClickUp" ClipToBounds="True"
                    MouseRightButtonDown="ShowContextMenu">
                <Image Width="{Binding Width}" Height="{Binding Height}" Stretch="Fill" HorizontalAlignment="Center" Source="{Binding BitMap}">
                    <Image.BitmapEffect>
                        <DropShadowBitmapEffect Color="Black" Direction="-50"
                    ShadowDepth="40" Softness=".7" />
                    </Image.BitmapEffect>
                </Image>
                
                <!-- Single selection rectangle.  Not a list -->
                <Rectangle Width="{Binding Path=SelWidth}" Height="{Binding Path=SelHeight}" Stroke="{Binding Path=SelColor}">
                    <Rectangle.RenderTransform>
                        <TranslateTransform X="{Binding Path=SelX}" Y="{Binding Path=SelY}"/>
                    </Rectangle.RenderTransform>
                </Rectangle>
                
                <ItemsControl ItemsSource="{Binding Path=TextBox}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Canvas/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Rectangle Tag="{Binding Path=Index}" Width="{Binding Path=Width}" Height="{Binding Path=Height}" Fill="{Binding Path=Color}">
                                <Rectangle.RenderTransform>
                                    <TranslateTransform X="{Binding Path=X}" Y="{Binding Path=Y}"/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

                <ItemsControl ItemsSource="{Binding Path=SelectedLines}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Canvas/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Rectangle Width="{Binding Path=Width}" Height="{Binding Path=Height}" Fill="{Binding Path=Color}">
                                <Rectangle.RenderTransform>
                                    <TranslateTransform X="{Binding Path=X}" Y="{Binding Path=Y}"/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

                <ItemsControl ItemsSource="{Binding Path=TextBlocks}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Canvas/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Rectangle Width="{Binding Path=Width}" Height="{Binding Path=Height}" Fill="{Binding Path=Color}" MouseLeave="ExitTextBlock" MouseEnter="EnterTextBlock">
                                <Rectangle.RenderTransform>
                                    <TranslateTransform X="{Binding Path=X}" Y="{Binding Path=Y}"/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

                <ItemsControl ItemsSource="{Binding Path=LinkBox}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Canvas/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Rectangle Tag="{Binding Path=Index}" Width="{Binding Path=Width}" Height="{Binding Path=Height}" Fill="{Binding Path=Color}" IsEnabled="True" MouseDown="LinkClick">
                                <Rectangle.RenderTransform>
                                    <TranslateTransform X="{Binding Path=X}" Y="{Binding Path=Y}"/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

            </Canvas>
        </DataTemplate> 

        <DataTemplate x:Key="ThumbTemplate">
            <Image Width="{Binding Width}" Height="{Binding Height}" Stretch="Fill" HorizontalAlignment="Center" Source="{Binding BitMap}" Margin="24,24,0,0">
                <Image.BitmapEffect>
                    <DropShadowBitmapEffect Color="Black" Direction="-50"
                    ShadowDepth="5" Softness=".7" />
                </Image.BitmapEffect>
            </Image>
        </DataTemplate>

        <Style TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Background" Value="Black" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!-- Customize listview to turn off selection -->
    </Window.Resources>

    <!-- The following is needed to set up all the keyboard short cuts -->
    <Window.CommandBindings>
        <CommandBinding Command="Open" Executed="OpenFileCommand"></CommandBinding>
        <CommandBinding Command="SelectAll" Executed="SelectAllCommand"></CommandBinding>
        <CommandBinding Command="Copy" Executed="CopyCommand"></CommandBinding>
        <CommandBinding Command="Print" Executed="PrintCommand"></CommandBinding>
        <CommandBinding Command="Help" Executed="HelpCommand"></CommandBinding>
        <CommandBinding Command="Close" Executed="CloseCommand"></CommandBinding>
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Key="O" Modifiers="Control" Command="Open"></KeyBinding>
        <KeyBinding Key="A" Modifiers="Control" Command="SelectAll"></KeyBinding>
        <KeyBinding Key="C" Modifiers="Control" Command="Copy"></KeyBinding>
        <KeyBinding Key="P" Modifiers="Control" Command="Print"></KeyBinding>
        <KeyBinding Key="W" Modifiers="Control" Command="Close"></KeyBinding>
        <KeyBinding Key="F1" Command="Help"></KeyBinding>
    </Window.InputBindings>

    <!-- To do add in command short cuts and bindings -->
    <DockPanel LastChildFill="True">
        <!-- Menu item at top -->
        <Menu IsMainMenu="True" DockPanel.Dock="Top">
            <MenuItem Header="_File" x:Name="xaml_file">
                <MenuItem Header="_Open..." Command="Open" InputGestureText="Ctrl+O">
                    <MenuItem.Icon>
                        <Image Source="Resources/OpenFile.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Save As..." x:Name="xaml_saveas">
                    <MenuItem.Icon>
                        <Image Source="Resources/saveHS.png" />
                    </MenuItem.Icon>
                    <MenuItem Header="PDF" Click="SavePDF" x:Name="xaml_savepdf"/>
                    <MenuItem Header="Linearized PDF" Click="Linearize" x:Name="xaml_linearize_pdf"/>
                    <MenuItem Header="PDF-1.3" Click="SavePDF13" x:Name="xaml_savepdf13"/>
                    <MenuItem Header="PDF/A" x:Name="xaml_savepdfa">
                        <MenuItem Header="PDF/A-1 RGB" Click="SavePDFA1_RGB" x:Name="xaml_savepdfa1_rgb"/>
                        <MenuItem Header="PDF/A-1 CMYK" Click="SavePDFA1_CMYK" x:Name="xaml_savepdfa1_cmyk"/>
                        <MenuItem Header="PDF/A-2 RGB" Click="SavePDFA2_RGB" x:Name="xaml_savepdfa2_rgb"/>
                        <MenuItem Header="PDF/A-2 CMYK" Click="SavePDFA2_CMYK" x:Name="xaml_savepdfa2_cmyk"/>
                    </MenuItem>
                    <MenuItem Header="PDF/X-3 Gray" Click="SavePDFX3_Gray" x:Name="xaml_savepdfx3_gray"/>
                    <MenuItem Header="PDF/X-3 CMYK" Click="SavePDFX3_CMYK" x:Name="xaml_savepdfx3_cmyk"/>
                    <MenuItem Header="PCL-XL" Click="SavePCLXL" x:Name="xaml_savepclxl"/>
                    <MenuItem Header="XPS" Click="SaveXPS"/>
                    <MenuItem Header="TEXT" Click="SaveText"/>
                    <MenuItem Header="HTML" Click="SaveHTML"/>
                    <MenuItem Header="XML" Click="SaveXML"/>
                </MenuItem>
                <MenuItem Header="_Close" Command="Close" InputGestureText="Ctrl+W" x:Name="xaml_closefile">
                    <MenuItem.Icon>
                        <Image Source="Resources/Close.ico" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Info" Click="ShowInfo" x:Name="xaml_showinfo">
                    <MenuItem.Icon>
                        <Image Source="Resources/info.png" />
                    </MenuItem.Icon>
                </MenuItem>
                <Separator />
                <MenuItem Header="Extract Selection..." x:Name="xaml_extractselection">
                    <MenuItem.Icon>
                        <Image Source="Resources/saveHS.png" />
                    </MenuItem.Icon>
                    <MenuItem Header="PDF" Click="ExtractPDF" x:Name="xaml_extractpdf"/>
                    <MenuItem Header="EPS" Click="ExtractEPS" x:Name="xaml_extracteps"/>
                    <MenuItem Header="PS" Click="ExtractPS" x:Name="xaml_extractps"/>
                </MenuItem>
                <MenuItem Header="Page Conversions..." Click="ConvertClick" x:Name="xaml_conversions">
                </MenuItem>
                <Separator />
                <MenuItem Header="Print..." Command="Print" InputGestureText="Ctrl+P" x:Name="xaml_print">
                    <MenuItem.Icon>
                        <Image Source="Resources/printer.ico" />
                    </MenuItem.Icon>
                </MenuItem>
                <Separator />
                <MenuItem Header="Show Messages" Click="ShowGSMessage" x:Name="xaml_gsmessage">
                    <MenuItem.Icon>
                        <Image Source="Resources/Message.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
            <MenuItem Header="_Edit" x:Name="xaml_edit">
                <MenuItem Header="Extract PDF Pages" Click="ExtractPages" x:Name="xaml_Extract">
                        <MenuItem.Icon>
                            <Image Source="Resources/saveHS.png" />
                        </MenuItem.Icon>
                </MenuItem>
                <Separator />
                <MenuItem Header="Copy Page" Click="CopyPage" x:Name="xaml_copypage">
                    <MenuItem.Icon>
                        <Image Source="Resources/copy.ico" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Paste Page To" x:Name="xaml_pastpageto">
                    <MenuItem Header="BMP" Click="PastePage" Tag="BMP"/>
                    <MenuItem Header="GIF" Click="PastePage" Tag="GIF"/>
                    <MenuItem Header="JPEG" Click="PastePage" Tag="JPG"/>
                    <MenuItem Header="PNG" Click="PastePage" Tag="PNG"/>
                    <MenuItem Header="TIFF" Click="PastePage" Tag="TIF"/>
                    <MenuItem Header="WDP (HD Photo)" Click="PastePage" Tag="WDP"/>
                </MenuItem>
                <Separator />
                <MenuItem Header="Select Text All" Command="SelectAll" InputGestureText="Ctrl+A" x:Name="xaml_selectalltext" />
                <MenuItem Header="Deselect Text" x:Name="xaml_deselecttext" />
                <MenuItem Header="Copy Text" Command="Copy" InputGestureText="Ctrl+C" x:Name="xaml_copytext" />
            </MenuItem>
            <MenuItem Header="_View" x:Name="xaml_view">
                <MenuItem Header="Next Page" Click="OnForwardPageClick"/>
                <MenuItem Header="Previous Page" Click="OnBackPageClick"/>
                <MenuItem Header="_Find" Click="Search"/>
            </MenuItem>
            <MenuItem Header="_Options">
                <MenuItem Header="Output Intents" Click="OutputIntents" />
                <MenuItem Header="Zoom Control" IsCheckable="true" Checked="ShowFooter" Unchecked="HideFooter"/>
                <MenuItem Header="Show Annotations" IsCheckable="True" IsChecked ="True"  Checked="AnnotationOn" Unchecked="AnnotationOff"/>
                <MenuItem Header="AntiAlias" >
                    <MenuItem x:Name="xaml_AA_High" Header="High" IsCheckable="True" IsChecked="True" Checked="OnAAChecked" />
                    <MenuItem x:Name="xaml_AA_MedHigh" Header="Medium High" IsCheckable="True" IsChecked="False" Checked="OnAAChecked"/>
                    <MenuItem x:Name="xaml_AA_Med" Header="Medium" IsCheckable="True" IsChecked="False" Checked="OnAAChecked"/>
                    <MenuItem x:Name="xaml_AA_Low" Header="Low" IsCheckable="True" IsChecked="False" Checked="OnAAChecked" />
                    <MenuItem x:Name="xaml_AA_None" Header="None" IsCheckable="True" IsChecked="False" Checked="OnAAChecked" />
                </MenuItem>
                
            </MenuItem>

            <MenuItem Header="_Help">
                <MenuItem Header="GSView Help" Command="Help" InputGestureText="F1"/>
                <MenuItem Header="About" Click="OnAboutClick"/>
            </MenuItem>
        </Menu>

            <!-- List of icons for page navigation, print, etc -->
        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Background="WhiteSmoke">
            <Button  x:Name="xaml_open" Width="20" Height="20" Click="OpenFile" Background="Transparent" BorderBrush="Transparent" Margin="10,0,0,0">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Width="20" Height="20">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="Resources/folder_open.ico"/>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button x:Name="xaml_SavePDF" Width="20" Height="20" Click="SavePDF" Background="Transparent" BorderBrush="Transparent" Margin="10,0,0,0">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Width="20" Height="20">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="Resources/FloppyDisk.ico"/>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button x:Name="xaml_Print" Margin="10 0 0 0" Width="20" Height="20" Click="Print" Background="Transparent" BorderBrush="Transparent">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Width="20" Height="20">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="Resources/printer.ico"/>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Rectangle Width="2" Height="20" Margin="10 0 0 0">
                <Rectangle.Fill>
                    <SolidColorBrush Color="DarkGray">
                    </SolidColorBrush>
                </Rectangle.Fill>
            </Rectangle>
            <Button  Width="20" Height="20" x:Name="xaml_BackPage" Click="OnBackPageClick" Background="Transparent" BorderBrush="Transparent" Margin="10,0,5,0">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse>
                                <Ellipse.Fill >
                                    <ImageBrush ImageSource="Resources/Left.ico"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button  Width="20" Height="20" x:Name="xaml_ForwardPage" Click="OnForwardPageClick" Background="Transparent" BorderBrush="Transparent">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse>
                                <Ellipse.Fill >
                                    <ImageBrush ImageSource="Resources/Right.ico"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <TextBox x:Name="xaml_currPage" Grid.Row="0" Width="25" Height="20" VerticalScrollBarVisibility="Hidden" 
                     HorizontalScrollBarVisibility="Hidden"  TextAlignment="Center" Margin="10,2,0,2" PreviewKeyDown="PageEnterClicked"/>
            <TextBlock Margin="5,0,0,0" Height="20" Text="/ 0" x:Name="xaml_TotalPages">
                <TextBlock.FontSize>12</TextBlock.FontSize>
            </TextBlock>
            <Rectangle Width="2" Height="20" Margin="10 0 0 0">
                <Rectangle.Fill>
                    <SolidColorBrush Color="DarkGray">
                    </SolidColorBrush>
                </Rectangle.Fill>
            </Rectangle>

            <Button  Margin="10 0 0 0" Width="20" Height="20" Click="ZoomIn" Background="Transparent" BorderBrush="Transparent" x:Name="xaml_zoomIn">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse>
                                <Ellipse.Fill >
                                    <ImageBrush ImageSource="Resources/zoom_in.ico"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button  Margin="10 0 0 0" x:Name="xaml_zoomOut" Width="20" Height="20" Click="ZoomOut" Background="Transparent" BorderBrush="Transparent">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse>
                                <Ellipse.Fill >
                                    <ImageBrush ImageSource="Resources/zoom_out.ico"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <TextBox Grid.Row="0"  Grid.Column="2" Width="30" Height="20" VerticalScrollBarVisibility="Hidden" 
                     HorizontalScrollBarVisibility="Hidden"  TextAlignment="Left" x:Name="xaml_Zoomsize"
                     Text="{Binding Mode=TwoWay, ElementName=xaml_ZoomSlider,Path=Value, UpdateSourceTrigger=PropertyChanged}" Margin="10 0 0 0" 
                     PreviewKeyDown="ZoomEnterClicked"/>
            <TextBlock Margin="5,0,0,0" Height="20" Text="%">
                <TextBlock.FontSize>12</TextBlock.FontSize>
            </TextBlock>

            <Button  Margin="10 0 0 0" Width="20" Height="20" Click="ActualSize" Background="Transparent" BorderBrush="Transparent" x:Name="xaml_ActualSize">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Width="20" Height="20">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="Resources/ActualSize48.png"/>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>

            <Button  Margin="10 0 0 0" Width="20" Height="20" Click="ContScrollFill" Background="Transparent" BorderBrush="Transparent" x:Name="xaml_ContScrollFill">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Width="20" Height="20">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="Resources/ContScrollFill48.png"/>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>

            <Button  Margin="10 0 0 0" Width="20" Height="20" Click="ExpandFill" Background="Transparent" BorderBrush="Transparent" x:Name="xaml_ExpandFill">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Width="20" Height="20">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="Resources/ExpandFill48.png"/>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>


            <Rectangle Width="2" Height="20" Margin="10 0 0 0">
                <Rectangle.Fill>
                    <SolidColorBrush Color="DarkGray">
                    </SolidColorBrush>
                </Rectangle.Fill>
            </Rectangle>
            <Button  Margin="10 0 0 0" x:Name="xaml_Thumbs" Width="20" Height="20" Click="ToggleThumbs" Background="Transparent" BorderBrush="Transparent">
                <Button.ToolTip>
                    <TextBlock Text="Thumbnails"/>
                </Button.ToolTip>
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse>
                                <Ellipse.Fill >
                                    <ImageBrush ImageSource="Resources/thumbnail.ico"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button  Margin="10 0 0 0" x:Name="xaml_Contents" Width="20" Height="20" Click="ToggleContents" Background="Transparent" BorderBrush="Transparent">
                <Button.ToolTip>
                    <TextBlock Text="Contents"/>
                </Button.ToolTip>
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Width="20" Height="20">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="Resources/contents.ico"/>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button  Margin="10 0 0 0" x:Name="xaml_Search" Width="20" Height="20" Click="Search" Background="Transparent" BorderBrush="Transparent">
                <Button.ToolTip>
                    <TextBlock Text="Search"/>
                </Button.ToolTip>
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Width="20" Height="20">
                                <Rectangle.Fill>
                                    <ImageBrush ImageSource="Resources/search.png"/>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button  Margin="10 0 0 0" x:Name="xaml_Links" Width="20" Height="20" Click="LinksToggle" Background="Transparent" BorderBrush="Transparent">
                <Button.ToolTip>
                    <TextBlock Text="Hyperlinks"/>
                </Button.ToolTip>
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse>
                                <Ellipse.Fill >
                                    <ImageBrush ImageSource="Resources/hyperlink.png"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </StackPanel>

        <!-- Search control -->
        <Grid x:Name="xaml_SearchControl" Background="WhiteSmoke" DockPanel.Dock="Bottom" Visibility="Collapsed">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Margin="5,5,0,5"><Bold>Search:</Bold></TextBlock>
            <TextBox x:Name="xaml_SearchText" Grid.Row="0"  Grid.Column="1" Width="100" Height="20" VerticalScrollBarVisibility="Hidden" 
                     HorizontalScrollBarVisibility="Hidden"  TextAlignment="Center" Margin="5,5,0,5"/>
            <Button Grid.Row="0" Grid.Column="2" Width="20" Height="20" Click="OnSearchBackClick" Background="Transparent" BorderBrush="Transparent" Margin="5,0,5,0">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse>
                                <Ellipse.Fill >
                                    <ImageBrush ImageSource="Resources/Left.ico"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button Grid.Row="0" Grid.Column="3" Width="20" Height="20" Click="OnSearchForwardClick" Background="Transparent" BorderBrush="Transparent">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse>
                                <Ellipse.Fill >
                                    <ImageBrush ImageSource="Resources/Right.ico"/>
                                </Ellipse.Fill>
                            </Ellipse>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </Grid>

        <!-- Footer control Currently just zoom slider in here but we may add more -->
        <Grid x:Name="xaml_FooterControl" Background="WhiteSmoke" DockPanel.Dock="Bottom" Visibility="Collapsed">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Row="0" Grid.Column="0" Margin="5,0,0,0"><Bold>Zoom:</Bold></TextBlock>
            <Slider x:Name="xaml_ZoomSlider" Grid.Row="0" Grid.Column="1" Margin="10, 0, 0, 0" 
                Width="150" Orientation="Horizontal" HorizontalAlignment="Center" 
                Value="100" Minimum="25" Maximum="400" 
                SmallChange="1" LargeChange="1"
                TickPlacement="None" TickFrequency="1"  IsSnapToTickEnabled="True"/>
        </Grid>

        <!-- The progress bar that runs while the thumbnails are rendered -->
        <Grid x:Name="xaml_ProgressGrid" DockPanel.Dock="Bottom" Visibility="Collapsed">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <ProgressBar x:Name="xaml_ThumbProgress" Grid.Row="0" Grid.Column="0" Margin="3" Minimum="0" 
                     Maximum="100" Height="20" HorizontalAlignment="Stretch" />
            <TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="5, 0, 5, 0"><Bold>Creating Thumbs</Bold></TextBlock>
           <!-- <Button  Grid.Row="0" Grid.Column="2" Width="50" Height="20" Name="xaml_CancelThumb" Click="CancelLoadClick" Background="Transparent" BorderBrush="Transparent" Margin="5,0,5,0">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Height="Auto" RadiusX="5" RadiusY="5">
                                <Rectangle.Fill >
                                    <SolidColorBrush Color="LightSlateGray"></SolidColorBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
                <TextBlock><Bold>Cancel</Bold></TextBlock>
            </Button> -->
        </Grid>
        
        <!-- The progress bar that runs during text search -->
        <Grid x:Name="xaml_SearchGrid" DockPanel.Dock="Bottom" Visibility="Collapsed">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <ProgressBar x:Name="xaml_SearchProgress" Grid.Row="0" Grid.Column="0" Margin="3" Minimum="0" 
                     Maximum="100" Height="20" HorizontalAlignment="Stretch" />
            <TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="5, 0, 5, 0"><Bold>Searching</Bold></TextBlock>
            <Button  Grid.Row="0" Grid.Column="2" Width="50" Height="20" Name="xaml_CancelSearch" Click="CancelSearchClick" Background="Transparent" BorderBrush="Transparent" Margin="5,0,5,0">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Height="Auto" RadiusX="5" RadiusY="5">
                                <Rectangle.Fill >
                                    <SolidColorBrush Color="LightSlateGray"></SolidColorBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
                <TextBlock><Bold>Cancel</Bold></TextBlock>
            </Button>
        </Grid>

        <!-- The progress bar that runs during copy all text to clipboard -->
        <Grid x:Name="xaml_CopyTextGrid" DockPanel.Dock="Bottom" Visibility="Collapsed">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <ProgressBar x:Name="xaml_CopyTextProgress" Grid.Row="0" Grid.Column="0" Margin="3" Minimum="0" 
                     Maximum="100" Height="20" HorizontalAlignment="Stretch" />
            <TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="5, 0, 5, 0"><Bold>Printing</Bold></TextBlock>
            <Button  Grid.Row="0" Grid.Column="2" Width="50" Height="20" Name="xaml_CancelCopyText" Click="CancelCopyText" Background="Transparent" BorderBrush="Transparent" Margin="5,0,5,0">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Height="Auto" RadiusX="5" RadiusY="5">
                                <Rectangle.Fill >
                                    <SolidColorBrush Color="LightSlateGray"></SolidColorBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
                <TextBlock><Bold>Cancel</Bold></TextBlock>
            </Button>
        </Grid>
        
        <!-- The progress bar that runs during printing -->
        <Grid x:Name="xaml_PrintGrid" DockPanel.Dock="Bottom" Visibility="Collapsed">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <ProgressBar x:Name="xaml_PrintProgress" Grid.Row="0" Grid.Column="0" Margin="3" Minimum="0" 
                     Maximum="100" Height="20" HorizontalAlignment="Stretch" />
            <TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="5, 0, 5, 0"><Bold>Printing</Bold></TextBlock>
            <!-- Asyc Cancel crashes in windows 8 for this xps creation -->
           <!-- <Button  Grid.Row="0" Grid.Column="2" Width="50" Height="20" Name="xaml_CancelPrint" Click="CancelPrintClick" Background="Transparent" BorderBrush="Transparent" Margin="5,0,5,0">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Height="Auto" RadiusX="5" RadiusY="5">
                                <Rectangle.Fill >
                                    <SolidColorBrush Color="LightSlateGray"></SolidColorBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
                <TextBlock><Bold>Cancel</Bold></TextBlock>
            </Button> -->
        </Grid>

        <!-- The progress bar that runs during MuPDF page by page conversion to file-->
        <Grid x:Name="xaml_MuPDFGrid" DockPanel.Dock="Bottom" Visibility="Collapsed">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <ProgressBar x:Name="xaml_MuPDFProgress" Grid.Row="0" Grid.Column="0" Margin="3" Minimum="0" 
                     Maximum="100" Height="20" HorizontalAlignment="Stretch"/>
            <TextBlock Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="5, 0, 5, 0"><Bold>MuPDF Converting Document</Bold></TextBlock>
            <Button  Grid.Row="0" Grid.Column="2" Width="50" Height="20" Name="xaml_CancelMuPDF" Click="CancelMuPDFClick" Background="Transparent" BorderBrush="Transparent" Margin="5,0,5,0">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Height="Auto" RadiusX="5" RadiusY="5">
                                <Rectangle.Fill >
                                    <SolidColorBrush Color="LightSlateGray"></SolidColorBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
                <TextBlock><Bold>Cancel</Bold></TextBlock>
            </Button>
        </Grid>
        
        <!-- The progress bar that runs during GS distilling -->
        <Grid x:Name="xaml_DistillGrid" DockPanel.Dock="Bottom" Visibility="Collapsed">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <ProgressBar x:Name="xaml_DistillProgress" Grid.Row="0" Grid.Column="0" Margin="3" Minimum="0" 
                     Maximum="100" Height="20" HorizontalAlignment="Stretch"/>
            <TextBlock x:Name="xaml_DistillName" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="5, 0, 5, 0"><Bold>Distilling</Bold></TextBlock>
            <Button  Grid.Row="0" Grid.Column="2" Width="50" Height="20" Name="xaml_CancelDistill" Click="CancelDistillClick" Background="Transparent" BorderBrush="Transparent" Margin="5,0,5,0">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Height="Auto" RadiusX="5" RadiusY="5">
                                <Rectangle.Fill >
                                    <SolidColorBrush Color="LightSlateGray"></SolidColorBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
                <TextBlock><Bold>Cancel</Bold></TextBlock>
            </Button>
        </Grid>

        <!-- Thumb viewer/selector -->
        <Grid x:Name="xaml_ThumbGrid" Width="100" Background="DarkGray" DockPanel.Dock="Left" Visibility="Collapsed">
            <ListView x:Name="xaml_ThumbList" HorizontalAlignment="Stretch" 
                      ItemTemplate="{StaticResource ThumbTemplate}" 
                      ScrollViewer.CanContentScroll="False" 
                      Background="DarkGray" 
                      PreviewMouseLeftButtonUp="ThumbSelected">
            </ListView>
        </Grid>

        <!-- Contents viewer/selector.  -->
        <Grid x:Name="xaml_ContentGrid" Width="250" Background="DarkGray" DockPanel.Dock="Left" Visibility="Collapsed">
            <ListView x:Name="xaml_ContentList" HorizontalAlignment="Stretch" 
                      ScrollViewer.CanContentScroll="False" PreviewMouseLeftButtonUp="ContentSelected"
                      Background="DarkGray" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="5,5,0,0" HorizontalAlignment="Left">
                            <TextBlock TextWrapping="Wrap" Text="{Binding StringMargin}" FontFamily="Segoe UI" FontSize="11" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>

        <Grid x:Name="xaml_OpenProgressGrid" HorizontalAlignment="Stretch" Background="DarkGray" DockPanel.Dock="Bottom" Visibility="Collapsed">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <TextBlock x:Name ="xaml_openfilestatus" Text="Opening File" Grid.Row="0" Grid.Column="0" Margin="0,0,0,10" HorizontalAlignment="Center"></TextBlock>
            <ProgressBar Grid.Row="1" Grid.Column="0" x:Name="xaml_OpenProgress" Minimum="0" 
                     Maximum="100" Height="20" Margin="100,0,100,200" HorizontalAlignment="Stretch" IsIndeterminate="True" />
        </Grid>

        <!-- The stock scroll bar that comes with the ListView scrollviewer does is not well behaved when
             the page sizes change.  So we have our own here that we will set up -->
        <ScrollBar x:Name="xaml_VerticalScroll" Orientation="Vertical" Grid.Row="0"  Grid.Column="1" DockPanel.Dock="Right"
           Width ="5" Height="Auto" Margin="0,0,0,0" Background="WhiteSmoke" Visibility="Collapsed" 
                   Scroll="VerticalScroll"/>

        <!-- Pages are last child fill.  This goes in the center of our dock panel ScrollViewer.CanContentScroll False allows continuous scrolling-->
        <!--  <Grid x:Name="xaml_PageGrid" HorizontalAlignment="Stretch" Background="DarkGray" DockPanel.Dock="Left" AllowDrop="True"> -->     
            <ListView x:Name="xaml_PageList" HorizontalAlignment="Stretch"
                      ItemTemplate="{StaticResource PageTemplate}" 
                      ScrollViewer.CanContentScroll="False"
                      Background="DarkGray"
                      ScrollViewer.ScrollChanged="ListViewScrollChanged"
                      MouseDoubleClick="PageDoubleClick" IsHitTestVisible="True"
                      SelectionMode="Single" 
                      PreviewMouseLeftButtonDown="ListPreviewMouseLeftButtonDown"
                      PreviewMouseLeftButtonUp="ListPreviewLeftButtonUp"
                      MouseLeave="ListMouseLeave"
                      PreviewMouseMove="ListPreviewMouseMove"
                      ScrollViewer.HorizontalScrollBarVisibility="Auto"
                      ScrollViewer.VerticalScrollBarVisibility="Hidden"
                      ScrollViewer.IsDeferredScrollingEnabled ="False" DockPanel.Dock="Left" AllowDrop="True"
                      >
                <!-- This keeps the pages in the center of the panel -->

            <ListView.ItemContainerStyle>
                                
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    <EventSetter Event="RequestBringIntoView" Handler="AvoidScrollIntoView"/>
                </Style>
                </ListView.ItemContainerStyle>
            </ListView>
        <!-- </Grid> --> 
    </DockPanel>
</Window>
